package com.example.demo.service.Impl;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.ao.External;
import com.example.demo.ao.MeterInfo;
import com.example.demo.ao.MeterInfoAo;
import com.example.demo.ao.PaymentByCodeAo;
import com.example.demo.mapper.PaymentByCodeMapper;
import com.example.demo.service.PaymentByCodeService;
import com.example.demo.utils.Sm4Util;
import com.example.demo.vo.MeterInfoVo;
import com.example.demo.vo.PaymentByCodeVo;
import com.example.demo.vo.RepairPhoneVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class PaymentByCodeServiceImpl implements PaymentByCodeService {

    @Autowired
    private PaymentByCodeMapper paymentByCodeMapper;

    @Override
    public List<PaymentByCodeVo> getPaymentByCodeInfo(PaymentByCodeAo paymentByCodeAo) {
        List<PaymentByCodeVo> paymentByCodeInfo = paymentByCodeMapper.getPaymentByCodeInfo(paymentByCodeAo);
        return paymentByCodeInfo;
    }

    @Override
    public int getResrcSuplCode(List<PaymentByCodeAo> paymentByCodeAo) {
        int num = 0;
        for (PaymentByCodeAo byCodeAo : paymentByCodeAo) {
            PaymentByCodeVo resrcSuplCode = paymentByCodeMapper.getResrcSuplCode(byCodeAo);
            //如果中台编码为空，插入
            if (BeanUtil.isEmpty(resrcSuplCode)) {
                num = paymentByCodeMapper.insertResrcSuplCode(byCodeAo);
            } else {
                num = paymentByCodeMapper.updateResrcSuplCode(byCodeAo);
            }
        }
        return num;
    }

    @Override
    public String getMeterInfo(External meterInfoAos) {
        log.info("入参内容：{}", meterInfoAos);
        Map<String, String> metreInfoMap = (HashMap) meterInfoAos.getData();
        String metreInfoAo = metreInfoMap.get("meterInfoAo");
        List<RepairPhoneVo> repairPhone = paymentByCodeMapper.getRepairPhone();
        List<MeterInfoVo> list = new ArrayList<>();
        //解密
        String decode = Sm4Util.getInstance().decryptBySM4ECB(metreInfoAo, Sm4Util.KEY);
        MeterInfoAo jsonObject = JSONObject.parseObject(decode, MeterInfoAo.class);
        log.debug("xml转实体类后的内容:{}", jsonObject);
        MeterInfo meterInfo = new MeterInfo();

        for (MeterInfo meterInfoAo : jsonObject.getMeterInfos()) {
            String data = JSON.toJSON(meterInfoAo).toString();
            if (StringUtils.isNotEmpty(meterInfoAo.getConsNo())){
                meterInfo.setConsNo(meterInfoAo.getConsNo());
            } else if (StringUtils.isNotEmpty(meterInfoAo.getConsName())) {
                meterInfo.setConsName(meterInfoAo.getConsName());
            } else if (StringUtils.isNotEmpty(meterInfoAo.getMeterBarCode())) {
                meterInfo.setMeterBarCode(meterInfoAo.getMeterBarCode());
            } else if (StringUtils.isNotEmpty(meterInfoAo.getHouseNumber())) {
                meterInfo.setHouseNumber(meterInfoAo.getHouseNumber());
            }
            meterInfo.setMgtOrgCode(meterInfoAo.getMgtOrgCode());
        }

        List<MeterInfoVo> meter = paymentByCodeMapper.getMeter(meterInfo);
        //根据台区编码关联
        for (MeterInfoVo meterInfoVo : meter) {
            desensitize(meterInfoVo);
            for (RepairPhoneVo repairPhoneVo : repairPhone) {
                if (BeanUtil.isNotEmpty(meterInfoVo.getTgNo()) && meterInfoVo.getTgNo().equals(repairPhoneVo.getResrcSuplCode())) {
                    meterInfoVo.setRepairPhone(repairPhoneVo.getRepairPhone());
                }
            }
            if (BeanUtil.isEmpty(meterInfoVo.getOrgNo())) {
                meterInfoVo.setOrgNo("");
            }


        }
        list.addAll(meter);

        log.info("出参内容：{}", list);
        String array = JSON.toJSON(list).toString();
        log.info("出参内容：{}", array);
        String encry = Sm4Util.getInstance().encryptBySM4ECB(array, Sm4Util.KEY);
        return encry;
    }


    private static void desensitize(MeterInfoVo meterInfoVo) {
        String consNo = meterInfoVo.getConsNo();
        String consName = meterInfoVo.getConsName();
        //对用户编号进行脱敏
        if (BeanUtil.isNotEmpty(consNo)) {
            meterInfoVo.setConsNoDese(consNo.substring(0, 1) + "***" + consNo.substring(consNo.length() - 3));
        }

        //对用户名称进行脱敏
        if (BeanUtil.isNotEmpty(consName)) {
            meterInfoVo.setConsNameDese(consName.substring(0, 1) + "***" + consName.substring(consName.length() - 1));
        }
    }



    @Override
    public Object decrypt(String meterInfoAos) {
        String jiemi = Sm4Util.getInstance().decryptBySM4ECB(meterInfoAos, Sm4Util.KEY);
        return jiemi;
    }











    public static void main(String[] args) {
        List<MeterInfo> list = new ArrayList<>();
        MeterInfo m1 = new MeterInfo();
        MeterInfoAo m2 = new MeterInfoAo();
        m1.setMeterBarCode("6530001000000234972598");
//        m1.setConsName("付学让(卡)");
        m1.setConsNo("6502195063033");
//        m1.setHouseNumber("1-2-102号");
//        m2.setPageNo(1);
//        m2.setPageSize(10);

//        MeterInfoAo m2 = new MeterInfoAo();
//        m2.setMeterBarCode("6530001000000420000000");
//        m2.setConsName("芦艳明");
//        m2.setConsNo("6500000038201");
//        m2.setHouseNumber("013号");
        list.add(m1);
        m2.setMeterInfos(list);
//        list.add(m2);
        String a = JSON.toJSON(m2).toString();
        System.out.println(a);
        String b = Sm4Util.getInstance().encryptBySM4ECB(a, Sm4Util.KEY);
        System.out.println(b);
//        String jiemi = Sm4Util.getInstance().decryptBySM4ECB("", Sm4Util.KEY);
//        List<MeterInfoAo> jsonObject = JSONObject.parseArray(jiemi, MeterInfoAo.class);
//        System.out.println(jsonObject);
//        List<MeterInfoAo> jsonObject = JSON.parseObject(jiemi,MeterInfoAo.class);
//        String string = jsonObject.toString();
//        System.out.println(jsonObject);
//
    }
}
